<script type="text/javascript">
$(function(){
	var pg = App.codex.macFlowProcessHandler.ui, pp = pg.params || {}
		, hd = pg.children('.head'), bd = pg.children('.body');
	
	var frm = pg.frm = {
		appCb: pg.seek('appCb'),
		codeCb: pg.seek('codeCb'),
		faceCb: pg.seek('faceCb'),
		methodCb: pg.seek('methodCb'),
		beanId: pg.seek('beanId')
	};

	var appGd = $('<div class="grid"></div>').mac('grid', {
		key: 'id',
		cols : [{
			field: 'name', width: 200,
			title: '应用名'
		}],
		loader: {
			url: '/soagov/app/list.do',
			params: { pageNo: 1, pageSize: 50 },
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.appCb.mac('combo', {
		keyField: 'id',
		displayField: 'name',
		multiSelect: false,
		allowInput: true,
		width: 532,
		boxLeft: 0,
		boxWidth: 532,
		boxHeight: 320,
		boxEl: appGd,
		onSelect: function(me, sk, sd){
			var r = sd[0] || {};
			if (r.id) {
				pp.appId = r.id;
			}
		}
	});
	frm.appCb.mac('combo_grid', {});

	var codeGd = $('<div class="grid"></div>').mac('grid', {
		key: 'id',
		cols : [{
			field: 'code', width: 320, title: '类名'
		}],
		loader: {
			url: App.ctx + '/mac/flow/process/list.do',
			params: { pageNo: 1, pageSize: 50 },
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.codeCb.mac('combo', {
		keyField: 'code',
		displayField: 'code',
		multiSelect: false,
		// allowInput: true,
		width: 532,
		boxLeft: 0,
		boxWidth: 532,
		boxHeight: 320,
		boxEl: codeGd,
		onSelect: function(me, sk, sd){
			var r = sd[0] || {};
			if (r.id) {
				pp.id = r.id;
				// if (r.type == 1) {
					// pp.type = r.type;
					// frm.key.val(r.key);
					// frm.key.removeAttr('disabled');
				//	return;
				// }
			}
			// pp.type = 0;
			// frm.key.val('default');
			// frm.key.attr('disabled', true);
		}
	});
	frm.codeCb.mac('combo_grid', {});
	
	var faceGd = $('<div class="grid"></div>').mac('grid', {
		key: 'code',
		cols : [{
			field: 'code', title : '接口英文名', width: 140,
			render: function(r){
				return $('<span class="a"></span>').click(function(){
					App.open({
						ctx: 'soagov',
						action: 'faceView',
						params: { id: r.id }
					});
				}).append(r.code);
			}
		},{
			field: 'name', title : '接口中文名', width: 240
		},{
			field: 'ver', title : '版本', width: 80
		}],
		loader: {
			url: '/soagov/face/list.do',
			params: { pageNo: 1, pageSize: 50 },
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.faceCb.mac('combo', {
		keyField: 'id',
		displayField: 'face',
		multiSelect: false,
		width: 532,
		boxLeft: 0,
		boxWidth: 532,
		boxHeight: 320,
		boxEl: faceGd,
		onSelect: function(me, sk, sd){
			var r = sd[0] || {};
			if (r.id) {
				pp.faceId = r.id;
				pp.faceCode = r.code;
				methodGd.val('');
				methodGd.load({ faceId: r.id });
			}
		}
	});
	frm.faceCb.mac('combo_grid', {});
	
	var methodGd = $('<div class="grid"></div>').mac('grid', {
		key: 'id',
		cols : [{
			field: 'code', title : '方法声明', width: 320
		},{
			field: 'memo', title : '方法说明', width: 320
		}],
		loader: {
			url: '/code/face/method/list.do',
			params: { pageNo: 1, pageSize: 50 },
			autoLoad: false
		},
		pagerLength: 10
	});
	frm.methodCb.mac('combo', {
		keyField: 'id',
		displayField: 'code',
		multiSelect: false,
		width: 806,
		boxLeft: 0,
		boxWidth: 806,
		boxHeight: 320,
		boxEl: methodGd,
		onSelect: function(me, sk, sd){
			var r = sd[0] || {};
			if (r.id) {
				pp.methodId = r.id;
				pp.methodCode = r.code;
				$.post(App.codex.ctx + '/method/param/list.do', {
					codeId: pp.id,
					key: 'mac/flow/ProcessHandler/' + r.id + '/',
					methodId: r.id,
					pageNo: 1,
					pageSize: 50
				}, function(rs){
					var ro = mac.eval(rs);
					if (ro.success) {
						init(ro.data, r);
					} else {
						mac.alert(ro.data, ro.params);
					}
				});
			}
		}
	});
	frm.methodCb.mac('combo_grid', {});

	function init(ro, method) {
		bd.empty();
		$.each(ro.list, function(i, a) {
			var pl = pg.seek('arg').clone(), gd = pl.seek('grid');
			//
			pl.find('form fieldset legend').text('参数' + i);
			pl.seek('argType').text(a.type);
			pl.attr('name', a.name).show();
			//
			gd.mac('grid', {
				cols : [{
					field: 'toObj', title : '目标对象', width: 120,
					render: function(r, tr, gd) {
						return $('<span class="a"></span>')//
							.append(r.toObj).attr('hint', r.toObjType);
					}
				},{
					field: 'toProp', title : '目标属性', width: 120,
					render: function(r, tr, gd) {
						return $('<span class="a"></span>')//
							.append(r.toProp).attr('hint', r.toPropType)
					}
				},{
					field: 'fromObj', title : '来源对象', width: 120,
					render: function(r, tr, gd) {
						return $('<span class="a"></span>')//
							.append(r.fromObj).attr('hint', r.fromObjType);
					}
				},{
					field: 'fromProp', title : '来源属性', width: 120
				},{
					field: 'id', title : '操作', width: 100,
					render: function(r, tr, gd) {
						var p = $('<div></div>');
						if (!$.inArray(r.fromType, ['java.lang.String'])) {
							$('<span class="a"></span>').click(function(){
								App.open({
									ctx: 'codex',
									action: 'beanMappingEdit',
									params: {
										codeId: pp.id,
										toObj: r.toObj,
										toType: r.toType,
										fromObj: r.fromObj
									}
								}, gd.load);
							}).append('映射').appendTo(p);
							p.append('<span>&nbsp;&nbsp;</span>');
						}
						$('<span class="a"></span>').click(function(){
							App.open({
								ctx: 'codex',
								action: 'beanMappingEdit',
								params: r
							}, gd.load);
						}).append('编辑').appendTo(p);
						p.append('<span>&nbsp;&nbsp;</span>');
						$('<span class="a"></span>').click(function(){
							mac.confirm('确定要 删除 此映射吗？', function(){
								$.post(App.codex.ctx + '/bean/mapping/delete.do', {
									id: r.id
								}, function(rs){
									var ro = mac.eval(rs);
									if (ro.success) {
										gd.load();
									} else {
										mac.alert(ro.data, ro.params);
									}
								});
							});
						}).append('删除').appendTo(p);
						return p;
					}
				}],
				loader: {
					url: App.codex.ctx + '/bean/mapping/list.do',
					params: {
						pageNo: 1, pageSize: 50,
						codeId: pp.id,
						key: 'mac/flow/ProcessHandler/' + method.id + '/' + i
					},
					autoLoad: true
				},
				pagerLength: 10
			}).adjust();
			
			// if (App.logon.power('code.mac.flow.save')){
				pl.seek('add').click(function(){
					// 添加前先保存
					var pms = getValues();
					if (!validate(pms)) {
						return;
					}
					$.post(App.codex.ctx + '/mac/flow/process/save.do', pms, function(rs){
						var ro = mac.eval(rs);
						if (ro.success) {
							pp.id = ro.data;
							App.open({
								ctx: 'codex',
								action: 'beanMappingAdd',
								params: {
									codeId: pp.id,
									key: 'mac/flow/ProcessHandler/' + method.id + '/' + i,
									toType: a.type
								}
							}, gd.load);
						} else {
							pg.seek('msg').html(mac.msg(ro.data, ro.params));
						}
					});
				});
			// }
			bd.append(pl);
		});
	}

	function adjust(){
		var h = pg.parent().height();
		bd.height(h - hd.height() - 48);
		// gd.adjust();
	}
	$(window).resize(adjust);
	adjust();

	if (pp.id) {
		var w = mac.wait('Loading');
		try {
			$.get(App.codex.ctx + '/mac/flow/process/get.do', {
				id: pp.id
			}, function(rs) {
				var ro = mac.eval(rs);
				if (ro.success) {
					setValues(ro.data);
				} else {
					pg.seek('msg').html(mac.msg(ro.data, ro.params));
				}
			});
		} finally {
			w.close();
		}
	}
	
	function getValues(){
		return $.extend(pp, {
			code: frm.codeCb.val(),
			beanId: frm.beanId.val()
		});
	}
	function setValues(o){
		pp = o;
		frm.codeCb.val(o.code);
		frm.faceCb.val(o.faceCode);
		frm.methodCb.val(o.methodCode);
	}

	function validate(p){
// 		var m = pg.seek('msg').html(''), v = App.auth.validation, x = App.validate;
// 		return x(v.func.code, p.code, m, '功能编号') &&
// 			x(v.func.name, p.name, m, '功能名称') &&
// 			x(v.func.status, p.status, m, '状态');
		return true;
	}

	pg.seek('submit').click(function(){
		var pms = getValues();
		if (!validate(pms)) {
			return;
		}
		mac.confirm('确定要生成并推送此代码吗？', function(){
			$.post(App.codex.ctx + '/mac/flow/process/submit.do', pms, function(rs){
				var ro = mac.eval(rs);
				if (ro.success) {
					App.open({
						closeOnEsc: 1,
						ctx: 'codex',
						action: 'genPreview',
						params: ro.data
					});
				} else {
					pg.seek('msg').html(mac.msg(ro.data, ro.params));
				}
			});
		});
		return false;
	});
});
</script>
<div class="head">
	<div class="top">
		<div class="title">ProcessHandler生成器</div>
		<button name="submit" class="fr">提交</button>
	</div>
	<div class="clear"></div>
	<div class="form">
		<div class="item">
			<div class="label">类名</div>
			<div class="combo fl" name="codeCb"></div>
		</div>
		<div class="clear"></div>
		<div class="item">
			<div class="label">目标接口</div>
			<div class="combo fl" name="faceCb"></div>
			<div class="label">目标Bean ID</div>
			<input type="text" name="beanId" />
		</div>
		<div class="clear"></div>
		<div class="item">
			<div class="label">目标方法</div>
			<div class="combo fl" name="methodCb"></div>
		</div>
		<div class="clear"></div>
	</div>
</div>
<div class="clear"></div>
<div class="body args"></div>
<div class="clear"></div>
<div class="arg hidden" name="arg">
	<fieldset>
		<legend>参数</legend>
		<div>
			<div class="label fl">类型</div>
			<div class="label fl" name="argType"></div>
			<button name="add" class="fr" >添加映射</button>
		</div>
		<div class="clear"></div>
		<div class="body">
			<div class="grid" name="grid"></div>
		</div>
	</fieldset>
</div>